﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Input;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Word = Microsoft.Office.Interop.Word;
using System.Web;



namespace VSTOTestProject
{
    /// <summary>
    /// Summary description for CodedUITest
    /// </summary>
    [CodedUITest]
    public class CodedUITest
    {
        private static Word.Application WordApp;
        private static Word.Document WordDoc;

        public CodedUITest()
        {
            Initialize();
        }

        [TestMethod]
        public void TC_HyperLink()
        {
            try
            {
                string currentDocPath = ConfigurationManager.AppSettings.Get("WordFilePath");
                UIMap_VSTO.DocPath = currentDocPath;

                //
                //VSTO Section
                //To search & Select a text from the Word Document
                //
                string searchText = ConfigurationManager.AppSettings.Get("SearchText");
                WordDoc.Range(WordDoc.Range().Text.IndexOf(searchText), WordDoc.Range().Text.IndexOf(searchText) + searchText.Length).Select();

                //To create a hyperlink to the selected text
                this.UIMapVSTO.CreateHyperLink(ConfigurationManager.AppSettings.Get("LinkAddress"));

                //
                //VSTO Section
                //To Verify whether a hyperlink was actually created
                bool flag = false;

                if (WordDoc.Hyperlinks.Count == 0)
                {
                    System.Threading.Thread.Sleep(25000);
                    Assert.AreEqual<bool>(true, WordDoc.Hyperlinks.Count > 0, "TC_HyperLink: Links not Identified");
                    flag = true;
                }
                else
                    flag = true;
                if (flag)
                {
                    System.Collections.IEnumerator links = WordDoc.Hyperlinks.GetEnumerator();
                    links.Reset();

                    for (int i = 0; i < WordDoc.Hyperlinks.Count; i++)
                    {
                        links.MoveNext();
                        Word.Hyperlink link = (Word.Hyperlink)links.Current;

                        if (link.Range.Text == searchText)
                        {
                            link.Follow();
                            Assert.AreEqual<string>(ConfigurationManager.AppSettings.Get("LinkAddress"), link.Address);
                        }
                    }
                }
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        [TestMethod]
        public void TC_DemoAddin()
        {
            string currentDocPath = ConfigurationManager.AppSettings.Get("WordFilePath");
            UIMap_VSTO.DocPath = currentDocPath;

            this.UIMapVSTO.ClickDemoAddinButton();

        }

        private void Initialize()
        {
            try
            {
                Process[] ps = Process.GetProcesses();
                foreach (Process p in ps)
                {
                    if (p.ProcessName.Contains("WORD"))
                    {
                        //p.Close();
                        //p.Dispose();
                        p.Kill();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                }
                WordApp = new Word.Application();

                WordApp.Visible = true;
                object filePathObject = (object)ConfigurationManager.AppSettings.Get("WordFilePath");
                WordDoc = WordApp.Documents.Open(ref filePathObject);
                WordApp.Visible = true;
            }
            catch (Exception ex)
            {
                Assert.Fail("Initialize: " + ex.ToString());
                Debug.WriteLine(ex.ToString());
                if (WordApp != null)
                {
                    WordDoc.Close();
                    WordApp.Quit();
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            //finally
            //{
            //    GC.Collect();
            //    GC.WaitForPendingFinalizers();
            //}
        }

        #region Additional test attributes

        // You can use the following additional attributes as you write your tests:

        ////Use TestInitialize to run code before running each test 
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{        
        //    // To generate code for this test, select "Generate Code for Coded UI TC_DemoAddin" from the shortcut menu and select one of the menu items.
        //    // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
        //}

        ////Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{        
        //    // To generate code for this test, select "Generate Code for Coded UI TC_DemoAddin" from the shortcut menu and select one of the menu items.
        //    // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
        //}

        #endregion

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
       
        private TestContext testContextInstance;

        public UIMap_VSTO UIMapVSTO
        {
            get
            {
                if ((this.map == null))
                {
                    this.map = new UIMap_VSTO();
                }

                return this.map;
            }
        }

        private UIMap_VSTO map;        
    }
}
